package com.credithc.signature.job.service;

import com.credithc.signature.common.mapper.SignatureLogExtMapper;
import com.credithc.signature.common.mapper.SignatureLogMapper;
import com.credithc.signature.common.po.SignatureLog;
import com.credithc.signature.common.po.SignatureLogExt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年06⽉28⽇<br/>
 * @version：v1.0.0<br>
 */
@Service
public class InfoService {

    @Autowired
    private SignatureLogMapper signatureLogMapper;

    @Autowired
    private SignatureLogExtMapper signatureLogExtMapper;


    /**
     * 查询总数
     *
     * @param example
     * @return
     */
    public int selectCountByExample(Example example) {
        return signatureLogMapper.selectCountByExample(example);
    }

    /**
     * 取模查询
     *
     * @param taskId
     * @param total
     * @param startTime
     * @param endTime
     * @param channelNo
     * @param statusList
     * @return
     */
    public List<SignatureLog> selectInfoByTaskId(int taskId, int total, Date startTime, Date endTime, String channelNo, List<Integer> statusList) {
        Example example = new Example(SignatureLog.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(channelNo)) {
            criteria.andEqualTo("channelNo", channelNo);
        }
        criteria.andGreaterThanOrEqualTo("createTime", startTime)
                .andLessThanOrEqualTo("createTime", endTime)
                .andIn("signStatus", statusList)
                .andCondition("MOD(id," + total + ")=" + taskId);
        return signatureLogMapper.selectByExample(example);
    }

    /**
     * 根据Id获取请求数据
     *
     * @param refId
     * @return
     */
    public SignatureLogExt selectInfoByRefId(Long refId, Date startTime) {
        Example example = new Example(SignatureLogExt.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThanOrEqualTo("createTime", startTime)
                .andEqualTo("id", refId);
        return signatureLogExtMapper.selectOneByExample(example);
    }
}
